package cn.javablog.service.impl;

import cn.javablog.Vo.BlogVo;
import cn.javablog.Vo.PageBlogVo;
import cn.javablog.entity.TBlog;
import cn.javablog.entity.TBlogTags;
import cn.javablog.mapper.TBlogMapper;
import cn.javablog.mapper.TBlogTagsMapper;
import cn.javablog.service.TBlogService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.shiro.util.Assert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AllenYe
 * @since 2020-09-14
 */
@AllArgsConstructor
@Transactional
@Service
public class TBlogServiceImpl extends ServiceImpl<TBlogMapper, TBlog> implements TBlogService {
    private final TBlogMapper tBlogMapper;
    private final TBlogTagsMapper tBlogTagsMapper;

    /**
     * 查询全部
     * @param pageBlogVo
     * @return
     */
    @Override
    public IPage selectAll(PageBlogVo pageBlogVo) {
        return null;
    }

    /**
     * 添加博客，并把博客id跟tag添加到tblogtag表中
     * @param blogVo
     * @return
     */
    @Override
    public int addBlog(BlogVo blogVo) {
        TBlog tBlog = new TBlog();
        tBlog.setTitle(blogVo.getTitle());
        tBlog.setContent(blogVo.getContent());
        tBlog.setFirstPicture("https://unsplash.it/100/100?image=1005");
        tBlog.setViews(0);
        tBlog.setAppreciation(true);
        tBlog.setShareStatement(true);
        tBlog.setCommentabled(true);
        tBlog.setPublished(true);
        tBlog.setFlag("1");
        tBlog.setRecommend(0);
        tBlog.setCreateTime(new Date());
        tBlog.setUpdateTime(new Date());
        tBlog.setTypeId(Long.valueOf(blogVo.getType_id()));
        tBlog.setUserId(Long.valueOf(blogVo.getUser_id()));
        tBlog.setTagIds(blogVo.getTag_ids());
        tBlog.setDescription(blogVo.getDescription());
        int obj = tBlogMapper.insert(tBlog);
        TBlogTags newTags = new TBlogTags();
        newTags.setBlogId(String.valueOf(tBlog.getId()));
        String str = blogVo.getTag_ids();
        if(blogVo.getTag_ids().indexOf(",")>0){
            String[] arr = str.split(","); // 用,分割
            for(int i=0;i<arr.length;i++){
                newTags.setTagId(Long.valueOf(arr[i]));
                tBlogTagsMapper.insert(newTags);
            }
        }else {
            newTags.setTagId(Long.valueOf(str));
            tBlogTagsMapper.insert(newTags);
        }
        return obj;
    }

    /**
     * 删除博客，并且清除t_blog_tags相关博客的数据
     * @param id
     * @return
     */
    @Override
    public int delBlog(int id) {
        TBlog blog = tBlogMapper.selectById(id);
        blog.setFlag("0");
        Assert.notNull(blog,"该博客不存在");
        int ids = tBlogTagsMapper.deleteTagBlog(id);
        return tBlogMapper.updateById(blog);
    }

    /**
     * 修改博客
     * @param blogVo
     * @return
     */
    @Override
    public int editBlog(BlogVo blogVo) {
        TBlog blog = tBlogMapper.selectAllById(blogVo.getId());
        Assert.notNull(blog,"该博客不存在");
        blog.setTitle(blogVo.getTitle());
        blog.setContent(blogVo.getContent());
        blog.setFirstPicture(blogVo.getFirst_picture());
        blog.setViews(blogVo.getViews());
        blog.setAppreciation(blogVo.isAppreciation());
        blog.setShareStatement(blogVo.isShare_statement());
        blog.setCommentabled(blogVo.isCommentabled());
        blog.setPublished(blogVo.isPublished());
        blog.setRecommend(blogVo.getRecommend());
        blog.setCreateTime(blogVo.getCreateTime());
        blog.setUpdateTime(new Date());
        blog.setTypeId(Long.valueOf(blogVo.getType_id()));
        blog.setUserId(Long.valueOf(blogVo.getUser_id()));
        blog.setTagIds(blogVo.getTag_ids());
        blog.setDescription(blogVo.getDescription());
        return tBlogMapper.update(blog,new QueryWrapper<TBlog>().eq("id",blogVo.getId()));
    }
}
